package com.itheima.a_axios;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 处理请求编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2. 获取请求参数
        String username = request.getParameter("username");
        String age = request.getParameter("age");

        System.out.println(username + "   " + age);

        //3. 响应数据到浏览器
        //response.getWriter().write("你好啊axios");
        response.getWriter().print("{\"username\":\"rose\",\"age\":20}");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 处理请求编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2. 获取请求参数
        String username = request.getParameter("username");
        String age = request.getParameter("age");

        System.out.println(username + "   " + age);

        //3. 响应数据到浏览器
        //response.getWriter().write("你好啊axios");
        response.getWriter().print("{\"username\":\"jack\",\"age\":18}");

    }
}